<extend name="Public:base" />
<block name="content">
<div class="page-header">
		<h1>
			{$breadcrumb2}
			<small>
				<i class="icon-double-angle-right"></i>
				{$crumbs}
			</small>
			
			<button name="send" form="form-blog" type="submit" style="float:right;"  class="btn btn-sm btn-primary">提交</button>
			
		</h1>
	</div>
	<div class="row">
	<div class="col-xs-12">	
		<div class="panel-body">
        <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-blog" class="form-horizontal">
        
        <notempty name="Think.get.id">
			<input name="blog_id" type="hidden" value="{$Think.get.id}" />
		</notempty>
          <ul class="nav nav-tabs">
            <li class="active"><a href="#tab-general" data-toggle="tab">常规项</a></li>
            <li><a href="#tab-image" data-toggle="tab">图片参数</a></li>            
          </ul>
          <div class="tab-content">
          	<!-- 常规 START -->
	          	<div class="tab-pane active" id="tab-general">
	          		
	          		<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-name2">博客标题：</label>
						<div class="col-sm-10">
							<input id="input-name2" class="form-control" type="text" placeholder="博客标题" name="title"  value="{$blog.title|default=''}" />
						</div>
					</div>
					<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-name2">内容摘要：</label>
						<div class="col-sm-10">
							<textarea id="input-meta-description2" class="form-control" placeholder="内容摘要" rows="5" name="summary">{$blog_content.summary|default=''}</textarea>
						</div>
					</div>
					<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-name2">作者：</label>
						<div class="col-sm-10">
							<input id="input-name2" class="form-control" type="text" placeholder="作者" name="author" value="<?php echo empty($blog['author'])?C('SITE_NAME'):$blog['author']; ?>" />
						</div>
					</div>
					
					<div class="form-group required">
		                <label class="col-sm-2 control-label" for="input-image">
		                <span title="" data-toggle="tooltip" data-original-title="上传600x300的图片">博客图片：</span>
		                </label>
		                
		                <div class="col-sm-10" id="thumb">
		                  <a href="#" data-toggle="image" class="img-thumbnail">
		                  	<img osctype="image" <if condition="isset($blog['image'])"> 
								src="__ROOT__{$blog.thumb_image}" 
								<else /> 
								src="__ROOT__/Common/image/no_image_100x100.jpg" 
								</if>  />
								</a>
		                  <input osctype="image_input" type="hidden" name="image" value="{$blog.image|default=''}" id="input-image" />
		            </div>
		            
		            </div>   
		            
		            <div class="form-group">
	                <label class="col-sm-2 control-label" for="input-category"><span data-toggle="tooltip" title="(自动完成)">博客分类</span></label>
	                <div class="col-sm-10">
	                  <input type="text" name="category" value="" placeholder="博客分类" id="input-category" class="form-control" />
	                  <div id="blog-category" class="well well-sm" style="height: 150px; overflow: auto;">
	                  <?php if(isset($blog_categories)){ ?>  
	                  <div id="product-category"><i class="icon-ban-circle"></i> <?php echo $blog_categories['title']; ?>
	                      <input type="hidden" name="blog_category" value="<?php echo $blog_categories['id']; ?>" />
	                  </div>
	                  <?php } ?> 
	                  </div>
	                </div>
	              </div>			
		            
		           <div class="form-group">
						<label class="col-sm-2 control-label">博客状态：</label>
						<div class="col-sm-10">
							<label class="radio-inline">
							<input type="radio" checked="checked" value="1" name="status">启用</label>
							<label class="radio-inline">
							<input type="radio" value="0" name="status">停用</label>
						</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label">可评论：</label>
						<div class="col-sm-10">
							<label class="radio-inline">
							<input type="radio" checked="checked" value="1" name="allow_reply">是</label>
							<label class="radio-inline">
							<input type="radio" value="0" name="allow_reply">否</label>
						</div>
					</div>
					
					<div class="form-group">
	                    <label class="col-sm-2 control-label" for="description">博客详情：</label>
	                    <div class="col-sm-10">
	                      <textarea name="content" id="description">{$blog_content.content|default=''}</textarea>	
	                    </div>
                  	</div>
					
				
					
					
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-meta-description2">Meta标签描述：</label>
						<div class="col-sm-10">
							<textarea id="input-meta-description2" class="form-control" placeholder="Meta标签描述" rows="5" name="meta_description">{$blog.meta_description|default=''}</textarea>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-meta-keyword2">Meta标签关键词：</label>
						<div class="col-sm-10">
							<textarea id="input-meta-keyword2" class="form-control" placeholder="Meta标签关键词" rows="5" name="meta_keywords">{$blog.meta_keywords|default=''}</textarea>
						</div>
					</div>
					
					
					
	          	</div>
	          	<!-- 常规 END -->

	          	
	          	<!-- 图片 START -->
	          	<div class="tab-pane" id="tab-image">
	          		<div class="table-responsive">
	                	<table id="images" class="table table-striped table-bordered table-hover">
		                  <thead>
		                    <tr>
		                      <td class="text-left">博客图片</td>
		                      <td class="text-right">图片描述</td>
		                      <td class="text-right">选项排序</td>
		                      <td></td>
		                    </tr>
		                  </thead>
		                	 <tbody>
                     <?php $image_row = 0; ?>
                    <?php if(isset($blog_images)){ ?> 
	                    <?php foreach ($blog_images as $blog_images) { ?>
		                    <tr id="image-row<?php echo $image_row; ?>">
		                      <td class="text-left"><a href="" id="thumb-image<?php echo $image_row; ?>" num="<?php echo $image_row; ?>" data-toggle="image" class="img-thumbnail"><img osctype="gallery_image<?php echo $image_row; ?>" src="<?php echo $blog_images['thumb']; ?>" alt="" title="" /></a><input osctype="gallery_image_input<?php echo $image_row; ?>" type="hidden" name="blog_images[<?php echo $image_row; ?>][image]" value="<?php echo $blog_images['image']; ?>" id="input-image<?php echo $image_row; ?>" /></td>
		                      <td class="text-right"><input type="text" name="blog_images[<?php echo $image_row; ?>][title]" value="<?php echo $blog_images['title']; ?>" class="form-control" /></td>
		                      <td class="text-right"><input type="text" name="blog_images[<?php echo $image_row; ?>][sort_order]" value="<?php echo $blog_images['sort_order']; ?>" class="form-control" /></td>
		                      <td class="text-left"><button type="button" onclick="$('#image-row<?php echo $image_row; ?>').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="icon-trash"></i></button></td>
		                    </tr>
	                    <?php $image_row++; ?>
	                    <?php } ?>
                  	<?php } ?>
                  </tbody>
		          </table>
		                <div><a  onclick="addImage();" class="add_image btn btn-primary ">添加图片</a></div>
                  </div>
	          	</div>
	          	<!-- 图片 END -->
        
          </div>
        </form>
	</div>
	</div>
</div>
</block>
<block name="javascript">
<style>
.table thead > tr > td, .table tbody > tr > td {
    vertical-align: middle;
}	
.table thead td span[data-toggle="tooltip"]:after, label.control-label span:after {
	font-family: FontAwesome;
	color: #1E91CF;
	content: "\f059";
	margin-left: 4px;
}
</style>	

<script src="__PUBLIC__/ckeditor/ckeditor.js"></script> 
<script src="__PUBLIC__/fileupload/jquery.ui.widget.js"></script>
<script src="__PUBLIC__/fileupload/jquery.fileupload.js"></script>
<script>
CKEDITOR.replace('description', {
	//filebrowserImageUploadUrl: '{:U("Article/ckupload")}',	
	toolbar: [
				[ 'Source', 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ],
				[ 'FontSize', 'TextColor', 'BGColor' ],
				[ 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock','Format']
			]	
});
	
	
$('input[name=\'category\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: '{:U("BlogCategory/autocomplete")}' +'/filter_name/'+  encodeURIComponent(request),
			dataType: 'json',			
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['category_id']
					}
				}));
			}
		});
	},
	'select': function(item) {
		$('input[name=\'category\']').val('');
		
		$('#blog-category' + item['value']).remove();
		
		$('#blog-category').append('<div id="blog-category' + item['value'] + '"><i class="icon-ban-circle"></i> ' + item['label'] + '<input type="hidden" name="blog_category" value="' + item['value'] + '" /></div>');	
	}
});	
$('#blog-category').delegate('.icon-ban-circle', 'click', function() {
	$(this).parent().remove();
});	
	
	var image_row = '<?php echo $image_row; ?>';

	function addImage() {
		html  = '<tr id="image-row' + image_row + '">';
		html += '  <td class="text-left"><a href="#" id="thumb-image' + image_row + '"data-toggle="image" num="'+image_row+'" class="img-thumbnail"><img osctype="gallery_image'+image_row+'" src="__ROOT__/Common/image/no_image_100x100.jpg" alt="" title="" /><input osctype="gallery_image_input'+image_row+'" type="hidden" name="blog_images[' + image_row + '][image]" value="" id="input-image' + image_row + '" /></td>';
		html += '  <td class="text-right"><input type="text" name="blog_images[' + image_row + '][title]" value="" class="form-control" /></td>';
		html += '  <td class="text-right"><input type="text" name="blog_images[' + image_row + '][sort_order]" value="'+image_row+'" class="form-control" /></td>';
		html += '  <td class="text-left"><button type="button" onclick="$(\'#image-row' + image_row  + '\').remove();" data-toggle="tooltip" class="btn btn-danger"><i class="icon-trash"></i></button></td>';
		html += '</tr>';
		
		$('#images tbody').append(html);
		
		image_row++;
	}
		
$(function(){
	
	<present name="Think.get.id">
			Oscshop.setValue("status", {$blog.status|default=1});		
			Oscshop.setValue("allow_reply", {$blog.allow_reply|default=1});		
	</present>
	
	
	// tooltips on hover button-upload
	$('[data-toggle=\'tooltip\']').tooltip({container: 'body', html: true});

	// Makes tooltips work on ajax generated content
	$(document).ajaxStop(function() {
		$('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
	});	
	
	
	
	$(document).delegate('a[data-toggle=\'image\']', 'click', function(e) {
		e.preventDefault();
		
		var index=$(this).attr('num');
				
		var element = this;
		
		if(index==undefined){
			$(element).popover({
				html: true,
				placement: 'right',
				trigger: 'manual',
				content: function() {
					return '<button type="button" id="thumb-image"  class="btn btn-primary"><i class="icon-edit"></i></button> <button type="button" id="button-clear" class="btn btn-danger"><i class="icon-trash"></i></button>';
				}
			});
		}else{
			$(element).popover({
				html: true,
				placement: 'right',
				trigger: 'manual',
				content: function() {
					return '<button type="button" n="'+index+'"  class="btn btn-primary button-image"><i class="icon-edit"></i></button> <button type="button" id="button-clear" class="btn btn-danger"><i class="icon-trash"></i></button>';
				}
			});
		}
		

		
		$(element).popover('toggle');	
		
		//博客图片
		$('#thumb-image').on('click', function() {		
			
			$('#modal-image').remove();
			
			$('#form-upload').remove();
			
			$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input osctype="btn_upload_image" type="file" name="file" /></form>');
	
			$('#form-upload input[name=\'file\']').trigger('click');
			
			$(element).popover('hide');
			
			$('[osctype="btn_upload_image"]').fileupload({
				
	        	dataType: 'json',
	            url: "{:U('Image/upload_image',array('dir'=>'blog'))}",
	            add: function(e, data) {
	                $parent = $('#thumb');
	                $input = $parent.find('[osctype="image_input"]');
	                $img = $parent.find('[osctype="image"]');
	                data.formData = {old_blog_images:$input.val()};
	                $img.attr('src', "__IMG__/loading.gif");
	                data.submit();
	            },
	            done: function (e,data) {
					
	            	var image=data.result;
	            	
	            	
	                $parent = $('#thumb');
	                $input = $parent.find('[osctype="image_input"]');
	                $img = $parent.find('[osctype="image"]');
	                if(image) {
	                   // $img.prev('i').hide();
	                    $img.attr('src', '__ROOT__'+image.image_thumb);
	                    $img.show();
	                    $input.val(image.image);
	                } else {
	                    alert('上传失败');
	                }
	            }
   		 });
		});

			
		//博客相册
		$('.button-image').on('click', function() {
			$('#modal-image').remove();
			
			$('#form-upload').remove();
			
			var i=$(this).attr('n');
			
			$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input osctype="btn_upload_image" type="file" name="file" /></form>');
	
			$('#form-upload input[name=\'file\']').trigger('click');
			
			$(element).popover('hide');
			
			$('[osctype="btn_upload_image"]').fileupload({
				
	        	dataType: 'json',
	            url: "{:U('Image/upload_image',array('dir'=>'blog_gallery'))}",
	            add: function(e, data) {
	                $parent = $('#image-row'+i);
	                $input = $parent.find('[osctype="gallery_image_input'+i+'"]');
	                $img = $parent.find('[osctype="gallery_image'+i+'"]');
	                data.formData = {old_gallery_image:$input.val()};
	                $img.attr('src', "__IMG__/loading.gif");
	                data.submit();
	            },
	            done: function (e,data) {
					
	            	var image=data.result;	            	
	            	
	                $parent = $('#image-row'+i);
	                $input = $parent.find('[osctype="gallery_image_input'+i+'"]');
	                $img = $parent.find('[osctype="gallery_image'+i+'"]');
	                if(image) {
	                   // $img.prev('i').hide();
	                    $img.attr('src', '__ROOT__'+image.image_thumb);
	                    $img.show();
	                    $input.val(image.image);
	                } else {
	                    alert('上传失败');
	                }
	            }
   		 });
			
			
			
		});
	
		$('#button-clear').on('click', function() {
			$(element).find('img').attr('src', $(element).find('img').attr('data-placeholder'));
			
			$(element).parent().find('input').attr('value', '');
	
			$(element).popover('hide');
		});
	});
});
	
</script>
</block>